/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commonbehavior.factory;

import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;

/**
 * Factory that can create and delete an {@link Activity}.
 * 
 * @author Remi Schnekenburger
 */
public class ActivityNodeFactory extends NodeHelper {
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public Element createUMLElement(String name, Element umlParent){
		Debug.debug(this,"Call Element ActivityNodeFactory.createElement(String name, Element parent)", channel);

		//this method is called when creating an element from the outline
		//hence the parent is a BehavioredClassifier
		Activity aActivity = UMLFactory.eINSTANCE.createActivity();
		aActivity.setName(name);
		((BehavioredClassifier)umlParent).getOwnedBehaviors().add(aActivity);
		
		return aActivity;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#createElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public Element createUMLElement(Element umlParent){
		Debug.debug(this,"Call Element ActivityNodeFactory.createElement(Element parent)", channel);

		return  createUMLElement(getNewName(((BehavioredClassifier)umlParent)), umlParent);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof BehavioredClassifier) {
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof BehavioredClassifier) {
			return ((BehavioredClassifier)umlParent).getOwnedBehaviors().add((Behavior)element);
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof BehavioredClassifier) {
			return ((BehavioredClassifier)umlParent).getOwnedBehaviors().remove(element);
		}
		return false;
	}
	
	/**
	 * Calculates a default name for the new element.
	 * 
	 * @param parent the parent of the element to create
	 * 
	 * @return the defaut name for the element
	 */
	private String getNewName(BehavioredClassifier parent) {
		String name = "Activity_0";
		boolean isUnique = false;
		// i <10000: avoid infinite loops
		for(int i=0; !isUnique && (i< 10000); i++ ) {
			name = "Activity_"+i;
			if(parent.getOwnedBehavior(name) == null) {
				isUnique = true;
			}
		}
		return name;
	}
}
